package com.eya.rabbitmq.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMq配置
 * <p>参考：https://blog.csdn.net/hry2015/article/details/79545157</p>
 *
 * @auther: luolin
 * @since: 2018/10/25 15:42
 */
@Configuration
public class RabbitConfigure {

	// 队列名称
	public final static String SPRING_BOOT_QUEUE            = "spring-boot-queue-3";
	public final static String SPRING_BOOT_QUEUE_OBJECT     = "spring-boot-queue-object";
	public final static String SPRING_BOOT_QUEUE_EXCHANGE_A = "spring-boot-queue-exchange-A";
	public final static String SPRING_BOOT_QUEUE_EXCHANGE_B = "spring-boot-queue-exchange-B";
	// 交换机名称
	public final static String SPRING_BOOT_EXCHANGE         = "spring-boot-exchange-2";
	// 绑定的值
	public static final String SPRING_BOOT_BIND_KEY         = "spring-boot-bind-key-2";

	// === 在RabbitMQ上创建queue,exchange,binding 方法一：通过@Bean实现 begin ===

	/**
	 * 定义队列：
	 */
	@Bean
	Queue queue() {
		return new Queue(SPRING_BOOT_QUEUE, false);
	}

	@Bean
	Queue queueObject() {
		return new Queue(SPRING_BOOT_QUEUE_OBJECT, false);
	}

	@Bean
	Queue queueExchangeA() {
		return new Queue(SPRING_BOOT_QUEUE_EXCHANGE_A, false);
	}

	@Bean
	Queue queueExchangeB() {
		return new Queue(SPRING_BOOT_QUEUE_EXCHANGE_B, false);
	}

	/**
	 * 定义交换机
	 */
	@Bean
	TopicExchange exchange() {
		return new TopicExchange(SPRING_BOOT_EXCHANGE, false, false);
	}


	/**
	 * 通过routingKey，将队列和交换机绑定，推送消息时，指定一个routingKey，交换机会根据绑定关系，将消息放入到具有绑定关系的队列中
	 * @param queueExchangeA
	 * @param exchange
	 * @return
	 */
	@Bean
	Binding bindingExchangeMessage4QueueA(Queue queueExchangeA, TopicExchange exchange) {
		return BindingBuilder.bind(queueExchangeA).to(exchange).with("topic.a");
	}

	/**
	 * #是通配符
	 * @param queueExchangeB
	 * @param exchange
	 * @return
	 */
	@Bean
	Binding bindingExchangeMessage(Queue queueExchangeB, TopicExchange exchange) {
		return BindingBuilder.bind(queueExchangeB).to(exchange).with("topic.#");
	}


}
